﻿using System.Runtime.InteropServices;
using System.Text.Json;
using BLL.DTO.RecipeDto;
using BLL.Manager;
using Helper;
using HZY.Framework.DependencyInjection;
using MiniExcelLibs;
using Model;
using Model.EntityInfo;

namespace WinFormsSprayProcessSCADASystem.Pages
{
    [ComVisible(true)]
    public class PageRecipeManage : IScopedSelfDependency
    {
        private readonly RecipeManager _recipeManager;
        private JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions()
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            WriteIndented = true
        };
        public PageRecipeManage(RecipeManager recipeManager)
        {
            _recipeManager = recipeManager;
        }

        //获取配方列表
        [ComVisible(true)]
        public async Task<string> GetAllList(string queryStr)
        {
            RecipeListDto data = JsonSerializer.Deserialize<RecipeListDto>(queryStr);
            BaseResult<ResPageData<RecipeInfoModel>> res = await _recipeManager.GetRecipeLidtAsync(data);
            string jsonResult = JsonSerializer.Serialize(res, new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true
            });
            return jsonResult;
        }

        //新增配方列表
        [ComVisible(true)]
        public async Task<string> Add(string queryStr)
        {
            RecipeAddDto data = JsonSerializer.Deserialize<RecipeAddDto>(queryStr);
            BaseResult<string> res = await _recipeManager.CreateRecipeAsync(data);
            string jsonResult = JsonSerializer.Serialize(res, new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true
            });
            return jsonResult;
        }


        //更新配方
        [ComVisible(true)]
        public async Task<string> Update(string queryStr)
        {
            RecipeUpdateDto data = JsonSerializer.Deserialize<RecipeUpdateDto>(queryStr, _jsonSerializerOptions);
            BaseResult<string> res = await _recipeManager.UpdateRecipeAsync(data);
            string jsonResult = JsonSerializer.Serialize(res, _jsonSerializerOptions);
            return jsonResult;
        }



        //删除配方
        [ComVisible(true)]
        public async Task<string> Del(string queryStr)
        {
            RecipeDelDto data = JsonSerializer.Deserialize<RecipeDelDto>(queryStr, _jsonSerializerOptions);
            BaseResult<string> res = await _recipeManager.DelRecipeAsync(data);
            string jsonResult = JsonSerializer.Serialize(res, _jsonSerializerOptions);
            return jsonResult;
        }

        //导入配方
        [ComVisible(true)]
        public async Task<string> ImportExcelRecipe()
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Excel 文件 (*.xlsx)|*.xlsx|Excel 97-2003 文件 (*.xls)|*.xls";
                openFileDialog.Title = "请选择配方";
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                openFileDialog.Multiselect = false; // 单选模式

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string selectedFilePath = openFileDialog.FileName; // 获取选中的文件路径

                    // 处理文件（例如读取内容）
                    try
                    {
                        List<RecipeAddDto> rows = MiniExcel.Query<RecipeAddDto>(selectedFilePath).ToList();
                        BaseResult<string> res = await _recipeManager.UpdateOrInsertRecipeAsync(rows);
                        string jsonResult = JsonSerializer.Serialize(res, _jsonSerializerOptions);
                        return jsonResult;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        string jsonResult = JsonSerializer.Serialize(new BaseResult<string>()
                        {
                            Code = SystemEnums.ResCode.Fail,
                            Data = "",
                            Msg = ex.Message
                        }, _jsonSerializerOptions);
                        return jsonResult;
                    }
                }
                else
                {
                    string jsonResult = JsonSerializer.Serialize(new BaseResult<string>()
                    {
                        Code = SystemEnums.ResCode.Fail,
                        Data = "",
                        Msg = ""
                    }, _jsonSerializerOptions);
                    return jsonResult;
                }
            }
        }
    }
}
